/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-11-26
 * Time: 23:27
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

using tools;

namespace Labb4.Event
{
	/// <summary>
	/// Manages events, updates them and removes them when done playing.
	/// 
	/// Implements the Singleton design pattern
	/// </summary>
	public sealed class EventManager : Manager
	{
		#region members
		
		/// <summary>
		/// The one instance of this calss
		/// </summary>
		private static EventManager				m_Instance = new EventManager();
		/// <summary>
		/// All the currently playing events
		/// </summary>
		private List<Event>						m_Events;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, initializes members
		/// </summary>
		private EventManager() :
			base()
		{
			m_Events = new List<Event>();
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Stops all active events and removes them
		/// </summary>
		public void Clear()
		{
			foreach(Event it in m_Events)
			{
				it.StopEvent();
			}
			
			m_Events.Clear();
			EventHandler.Instance.Clear();
		}
		
		/// <summary>
		/// Creates an event according to the contents of a file
		/// </summary>
		/// <param name="name">The name of the config file holding info on the event</param>
		/// <returns>The event asked for</returns>
		public Event GetEvent(string eventConfigFile)
		{
			return new Event(eventConfigFile);
		}
		
		/// <summary>
		/// Adds an event
		/// </summary>
		/// <param name="e"></param>
		public void AddEvent(Event e)
		{
			m_Events.Add(e);
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// Updates all events, if an event has played to its end, it's removed
		/// </summary>
		/// <param name="deltaTime"></param>
		protected override void DoUpdate(ref int deltaTime)
		{
			List<Event> grimReaper = new List<Event>();
			
			foreach(Event it in m_Events)
			{
				it.UpdateEvent(ref deltaTime);
				
				if(it.HasPlayed)
				{
					grimReaper.Add(it);
				}
			}
			
			foreach(Event it in grimReaper)
			{
				m_Events.Remove(it);
			}
		}
		
		#endregion
		
		#region properties
		
		/// <summary>
		/// The one instance (get)
		/// </summary>
		public static EventManager Instance
		{
			get
			{
				return m_Instance;
			}
		}
		
		#endregion
	}
}
